﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web.UI.WebControls;

namespace WeChatParking.Util
{
    public static class Comm
    {
        #region 时间戳
        
        /// <summary>
        /// 获取时间戳。1970-1-1至今
        /// </summary>
        /// <returns></returns>
        public static long GetTimeStamp()
        {
            return GetTimeStamp(DateTime.Now);
        }
        /// <summary>
        /// 获取字符串MD5哈希值
        /// </summary>
        public static string GetMD5Hash(string targetValue)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hashVal = oMD5Hasher.ComputeHash(System.Text.Encoding.UTF8.GetBytes(targetValue));
            return System.BitConverter.ToString(hashVal).Replace("-", string.Empty);
            //return FormsAuthentication.HashPasswordForStoringInConfigFile(targetValue, "md5");
        }
        /// <summary>
        /// 获取时间戳（1970-1-1至今的秒数）
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static long GetTimeStamp(DateTime datetime)
        {
            TimeSpan ts = datetime - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds);
        } 

        /// <summary>
        /// 获取时间戳（1970-1-1至今的秒数）对应的时间
        /// </summary>
        /// <returns></returns>
        public static DateTime GetDateTimeFromTimeStamp(long timeStamp)
        {
            return new DateTime(1970, 1, 1).AddSeconds(timeStamp);
        }

        /// <summary>
        /// 获取时间戳（1970-1-1至今的秒数）对应的时间
        /// </summary>
        public static DateTime GetDateTimeFromTimeStamp(string timeStamp)
        {
            return GetDateTimeFromTimeStamp(long.Parse(timeStamp));
        }
        #endregion


        /// <summary>
        /// 数据流转换成字符串
        /// </summary>
        public static string Stream2Str(System.IO.Stream stream)
        {
            byte[] b = new byte[stream.Length];
            stream.Read(b, 0, (int)stream.Length);
            string resultStr = System.Text.Encoding.UTF8.GetString(b);
            return resultStr;
        }
        /// <summary>
        /// Unicode转字符串
        /// </summary>
        /// <returns>The to string.</returns>
        /// <param name="unicode">Unicode.</param>
        public static string UnicodeToString(string unicode)
        {
            string resultStr = "";
            string[] strList = unicode.Split('u');
            for (int i = 1; i < strList.Length; i++)
            {
                resultStr += (char)int.Parse(strList[i], System.Globalization.NumberStyles.HexNumber);
            }
            return resultStr;
        }
        /// <summary>
        /// 字符串转Unicode码
        /// </summary>
        /// <returns>The to unicode.</returns>
        /// <param name="value">Value.</param>
        public static string StringToUnicode(string value)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(value);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < bytes.Length; i += 2)
            {
                // 取两个字符，每个字符都是右对齐。
                stringBuilder.AppendFormat("u{0}{1}", bytes[i + 1].ToString("x").PadLeft(2, '0'), bytes[i].ToString("x").PadLeft(2, '0'));
            }
            return stringBuilder.ToString();
        }
      
    }
}
